<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class QuestionNaireUser extends Model
{
    //
    protected $table = 'htf_questionnaire_user';
    public $timestamps = false;
    protected $guarded=[];

    public function insertUserRecord($entry,$uid = '1')
    {
        foreach($entry as $k => $v)
        {
            if(is_array($v))
            {
                foreach($v as $k2 => $v2)
                {
                    $data[] = [
                        'q_id' => substr($k,6),
                        'u_id' => $uid,
                        'option' => $v2,
                        'cash_id' => env('CASH_ID'),
                        'created_at' => date('Y-m-d H:i:s')
                    ];
                }
            }
            else
            {
                $data[] = [
                    'q_id' => substr($k,6),
                    'u_id' => $uid,
                    'option' => $v,
                    'cash_id' => env('CASH_ID'),
                    'created_at' => date('Y-m-d H:i:s')
                ];
            }
        }
        $re = DB::table($this->table)->insert($data);
        //dump($re);
        //exit;

        return $re;
    }

    /**
     * 返回计算的配置文件
     * @param string $cashId
     * @param string $uid
     * @return mixed
     * create by wenQing
     */
    public function countRecordOption($cashId = '1',$uid = '1')
    {
        $arr = DB::table('htf_questionnaire as q')
                ->leftJoin('htf_questionnaire_user as qu','q.id','=','qu.q_id')
            ->leftJoin('htf_questionnaire_option as qo','qo.id','=','qu.option')
            ->where('qu.u_id','=',$uid)
            ->where('q.count_type','<>','0')
            ->select('qu.u_id','qu.q_id','qu.option','qo.name','q.count_type')
            ->get();

        foreach($arr as $type)
        {
            //是否男女
            if($type->count_type == '1')
            {
                $sexSelect = ($type->name == '男' ? '1' : '0');
            }
            elseif($type->count_type == '2')
            {
                $hereditySelect = ($type->name == '是' ? '1' : '0');
            }
            elseif($type->count_type == '3')
            {
                $levelSelect = mb_substr($type->name,2,1);
            }
        }
        $select = $sexSelect.$hereditySelect.$levelSelect; //生成配置的信息
        $record = config('question.'.$select);

        return [
            'point' => $record,
            'level' => $levelSelect
        ];
    }
}
